﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace trimmer
{

    class Processor
    {

        int numTexts = 0;
   

        public void Process()
        {
            Console.WriteLine("test of Выхрючиватель 0.1");
            DateTime dt_start = DateTime.Now;
            
            DirectoryInfo di = new DirectoryInfo("./in");
            FileInfo[] Files = di.GetFiles();
            try
            {
                if (Directory.Exists("./out")) Directory.Delete("./out", true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("./out :" + ex.Message);
            }
            try { Directory.CreateDirectory("./out"); }
            catch (Exception ex) { Console.WriteLine("./out :" + ex.Message); };


            foreach (FileInfo fi in Files)
            {
                ProcessFile(fi);
            }
            //global results
           
            
        }


        bool isNessInScope(string str)
        {
            str = str.ToLower();
            if (str.Contains("governess")) return false;
            if (str.Contains("witness")) return false;
            if (str.Contains("harness")) return false;
            if (str.Contains("patroness")) return false;
            if (str.Contains("baroness")) return false;
            if (str.Contains("marchioness")) return false;
            if (str.Contains("championess")) return false;
            if (str.Contains("lioness")) return false;
            return str.Contains("ness");
            //governess witness harness patroness baroness
        }


        void ProcessContext(int left,int right, XmlNodeList nodes, TextWriter wr )
        {
            XmlDocument outDoc = new XmlDocument();
            XmlNode newNode = outDoc.CreateElement("context");

            int numNesses=0;
            List<string> roots = new List<string>();
            List<string> sems = new List<string>();
            bool rootsFound = false;
            bool semFound = false;
            for (int i = left; i <= right; i++)
            {
                
                string str = nodes[i].InnerText.Trim();
                if (isNessInScope(str))
                {
                    roots.Add(str.Substring(0,3));
                    try
                    {
                        sems.Add(nodes[i].Attributes["sem"].Value);
                    }catch(Exception){}
                }
                

                //create set of sems and roots
            }
            for (int i = left; i <= right; i++)
            {
                if (isNessInScope(nodes[i].InnerText))
                {
                    numNesses++;
                    newNode.InnerXml += "<ness>"+nodes[i].OuterXml + "</ness> ";
                }

                    else
                {//roots
                    string lbr = "";
                    string rbr = "";
                    string tok=nodes[i].InnerText.Trim();
                    if (tok.Length>3)
                    if (roots.Contains(tok.Substring(0,3)))
                    {
                        rootsFound = true;
                        lbr = "<root>";
                        rbr = "</root>";
                    }
                    string sem = "###";
                    try { sem = nodes[i].Attributes["sem"].Value; }
                    catch { }
                    if (sems.Contains(sem))
                    {
                        semFound = true;
                        lbr = "<theme>"+lbr;
                        rbr = rbr+"</theme>";
                    }
                    if (nodes[i].InnerText.Contains("@@@"))
                        newNode.InnerXml += "\n"; else
                    newNode.InnerXml += lbr+ nodes[i].OuterXml + rbr +" ";
                }

            }
            if (numNesses > 1)
            {
                XmlNode mult = outDoc.CreateElement("multiple");
                mult.AppendChild(newNode);
                newNode = mult;
            }
            if (rootsFound)
            {
                XmlNode mult = outDoc.CreateElement("word-formation");
                mult.AppendChild(newNode);
                newNode = mult;
            }
            wr.Write(newNode.OuterXml);
         //   wr.WriteLine("\n");

        }
        void ProcessFile(FileInfo fi)
        {
            Console.WriteLine("\nProcessing " + fi.Name);

            XmlDocument inDoc = new XmlDocument();
            inDoc.Load(fi.FullName);



            XmlNodeList nodes = inDoc.SelectNodes("//s");
            int lastNess = 0;
            int posLeftNessInContext = 0;
            int posLastContextRight = 0;

            int contextSize = 120;
            int nessCount = 0;
		string nodeMaxima="";

            TextWriter wr = new StreamWriter("./out/" + fi.Name);
            wr.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<text>");

            for (int i = 0; i < nodes.Count; i++)
            {
                if ((i % 1000) == 0)
                {
                    Console.WriteLine(i.ToString() + " of " + nodes.Count.ToString());
                }

		if (nodes[i].OuterXml.Length>nodeMaxima.Length)
			nodeMaxima=nodes[i].OuterXml;     
        //           wr.Write(nodes[i].OuterXml);
	//	wr.WriteLine("\n");
                //Console.WriteLine(nodes[i].InnerText);

            }
          wr.WriteLine(nodeMaxima);  

            wr.WriteLine("</text>");
            wr.Close();
            // remove all b,

        }// end processfile
    }

}
